<?php
namespace app\api\controller\wanlshop;

use app\common\controller\Api;
use fast\Random;
use think\Db;
use WanlPay\Yansongda\Log;

/**
 * WanlShop优惠券接口
 */
class Coupon extends Api
{
    // protected $noNeedLogin = ['getList', 'query'];
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 获取优惠券列表
     *
     * @ApiSummary  (WanlShop 优惠券接口获取优惠券列表)
     * @ApiMethod   (GET)
     * 2020年9月15日17:44:57
     *
     * @param string $type 类型
     */
    public function getList($type = null)
    {
        $list = model('app\api\model\wanlshop\Coupon')
            ->where([
                'type' => $type,
                // 'rangetype' => 'all', 1.0.5升级
                'invalid' => 0
            ])
            ->order('createtime desc')
            ->paginate()
            ->each(function($order, $key){
                $order['shop'] = $order->shop?$order->shop->visible(['shopname']):[];
                return $order;
            });
        $list?($this->success('ok',$list)):($this->error(__('网络繁忙')));
    }

    /**
     * 查询我的优惠券
     *
     * @ApiSummary  (WanlShop 优惠券接口查询我的优惠券)
     * @ApiMethod   (GET)
     * 2020年9月16日03:32:43
     *
     * @param string $goods_id 商品ID
     * @param string $shop_id 店铺ID
     * @param string $shop_category_id 分类ID
     * @param string $price 价格
     */
    public function query($goods_id = null, $shop_id = null, $shop_category_id = null, $price = null)
    {
        $user_coupon = [];
        if ($this->auth->isLogin()) {
            foreach (model('app\api\model\wanlshop\CouponReceive')->where([
                'user_id' => $this->auth->id,
                'shop_id' => $shop_id,
                'price' => $price,
                'state' => '1'
            ])->select() as $row) {
                $user_coupon[$row['coupon_id']] = $row;
            }
        }
        // 开始查询 方案一
        $list = [];
        $goods_id = explode(",",$goods_id);
        $shop_category_id = explode(",",$shop_category_id);
        //要追加一个排序 选出一个性价比最高的
        foreach (model('app\api\model\wanlshop\Coupon')->where([
            'shop_id' => $shop_id,
            'price' => $price,
            //  'state'=>true
        ])->select() as $row) {
            // 筛选出还未开始的
            if(!($row['pretype'] == 'fixed' && (strtotime($row['startdate']) >= time() || strtotime($row['enddate']) < time()))){
                //追加字段
                $row['choice'] = false;
                // 检查指定的键名是否存在于数组中
                if(array_key_exists($row['id'], $user_coupon)){
                    $row['invalid'] = 0; // 强制转换优惠券状态
                    $row['id'] = $user_coupon[$row['id']]['id'];
                    $row['state'] = true;
                }else{
                    continue;
                    // 	$row['state'] = false;
                }
                // 排除失效优惠券
                if($row['invalid'] == 0){
                    // 高级查询，比较数组，返回交集(array_intersect)如果和原数据数目相同则加入
                    if($row['rangetype'] == 'all'){
                        $list[] = $row;
                    }
                    if($row['rangetype'] == 'goods' && count($goods_id) == count(array_intersect($goods_id, explode(",",$row['range'])))){
                        $list[] = $row;
                    }
                    // 1.1.0升级
                    if($row['rangetype'] == 'category'){
                        // 判断优化全类目是否在商品类目中，explode(',', $row['range'])[0] 目的向前兼容
                        if( in_array(explode(',', $row['range'])[0], $shop_category_id) ){
                            $list[] = $row;
                        }
                    }
                }
            }
        }
        // 开始查询 二
        // $list = [];
        // //要追加一个排序 选出一个性价比最高的
        // foreach (model('app\api\model\wanlshop\Coupon')->where([
        // 	'shop_id' => $shop_id,
        // 	'limit' => ['<=', intval($price)]
        // ])->select() as $row) {
        // 	// 如果优惠券状态有效  或  无效且我的列表中存在的
        // 	if($row['invalid'] == 0 || ($row['invalid'] == 1 && array_key_exists($row['id'], $user_coupon))){
        // 		// 筛选出过期的
        // 		if(!($row['pretype'] == 'fixed' && strtotime($row['startdate']) >= time())){
        // 			// 高级查询，比较数组，返回交集如果和原数据数目相同则加入
        // 			if($row['rangetype'] == 'goods'){
        // 				$goods_id = explode(",",$goods_id);
        // 				if(count($goods_id) == count(array_intersect($goods_id, explode(",",$row['range'])))){
        // 					// 检查指定的键名是否存在于数组中
        // 					if(array_key_exists($row['id'], $user_coupon)){
        // 						$row['id'] = $user_coupon[$row['id']]['id'];
        // 						$row['state'] = true;
        // 					}else{
        // 						$row['state'] = false;
        // 					}
        // 					$row['choice'] = false;
        // 					$list[] = $row;
        // 				}
        // 			}else if($row['rangetype'] == 'category'){
        // 				$shop_category_id = explode(",",$shop_category_id);
        // 				if(count($shop_category_id) == count(array_intersect($shop_category_id, explode(",",$row['range'])))){
        // 					if(array_key_exists($row['id'], $user_coupon)){
        // 						$row['id'] = $user_coupon[$row['id']]['id'];
        // 						$row['state'] = true;
        // 					}else{
        // 						$row['state'] = false;
        // 					}
        // 					$row['choice'] = false;
        // 					$list[] = $row;
        // 				}
        // 			}else{
        // 				if(array_key_exists($row['id'], $user_coupon)){
        // 					$row['id'] = $user_coupon[$row['id']]['id'];
        // 					$row['state'] = true;
        // 				}else{
        // 					$row['state'] = false;
        // 				}
        // 				$row['choice'] = false;
        // 				$list[] = $row;
        // 			}
        // 		}
        // 	}
        // }
        return $this->success('ok', $list);
    }

    /**
     * 获取我的优惠券列表
     *
     * @ApiSummary  (WanlShop 优惠券接口获取我的优惠券列表)
     * @ApiMethod   (GET)
     * 2020年9月16日08:09:17
     *
     * @param string $state 类型
     */
    public function getMyList($state = null)
    {
        if(empty($state) || $state != 1 && $state != 2){
            $page = input("param.page");
            if(!$page){
                $page = 1;
            }
            $page1 = $page-1;
            $list = [];
            $row = db("wanlshop_coupon_donate_log")
                ->where("sender_id|receiver_id",$this->auth->id)
                ->limit($page1,15)
                ->order("createtime desc")
                ->select();
            $count = db("wanlshop_coupon_donate_log")
                ->where("sender_id|receiver_id",$this->auth->id)
                ->count();
//            return json($row);
            $last_page = ceil($count/15);
            foreach ($row as $k => $v){

                $coupon = model('app\api\model\wanlshop\CouponReceive')
                    ->where("id",$v["coupon_id"])
                    ->field("coupon_id,coupon_no,user_id,shop_id,order_id,type,name,userlevel,usertype,price,remark,discount,limit,validity")
                    ->find();
                if($coupon){
                    $row[$k] = $coupon;
                    $user_id = 0;
                    if($v["sender_id"] == $this->auth->id){
                        $row[$k]["log_type"] = "send";
                        $user_id = $v["receiver_id"];
                    }
                    if($v["receiver_id"] == $this->auth->id){
                        $row[$k]["log_type"] = "receive";
                        $user_id = $v["sender_id"];
                    }
                    $user11 = db("user")->where("id",$user_id)->field("username,nickname,mobile")->find();
                    if($user11){
                        $user11["mobile"] = $this->desensitizeMobile($user11["mobile"]);
                        $user11["username"] = $this->desensitizeMobile($user11["mobile"]);
                        $row[$k]["user"] = $user11;
                    }
                    $row[$k]["createtime"] = date("Y-m-d H:i:s",$v["createtime"]);
                    $row[$k]["shop"] = [];
                }
            }
            if(count($row) == 0){
                $list = [
                    "current_page" => (int)$page,
                    "last_page" => $last_page,
                    "per_page" => 15,
                    "total" => $count,
                    "data" =>[]
                ];
            }else{
                $list = [
                    "current_page" => (int)$page,
                    "last_page" => $last_page,
                    "per_page" => 15,
                    "total" => $count,
                    "data" =>$row
                ];
            }
        }else{
            $list = model('app\api\model\wanlshop\CouponReceive')
                ->where([
                    'state' => $state,
                    'user_id' => $this->auth->id
                ])
                ->order('createtime desc')
                ->paginate()
                ->each(function($order, $key){
                    $order['shop'] = $order->shop?$order->shop->visible(['shopname']):[];
                    return $order;
                });
        }
        $list?($this->success('ok',$list)):($this->error(__('网络繁忙')));
    }

    private function desensitizeMobile($mobile)
    {
        if (strlen($mobile) != 11) {
            return $mobile; // 如果手机号长度不是11位，直接返回原手机号
        }
        return substr($mobile, 0, 3) . '****' . substr($mobile, 7);
    }

    /**
     * 领取优惠券
     *
     * @ApiSummary  (WanlShop 优惠券接口领取优惠券)
     * @ApiMethod   (POST)
     * 2020年9月16日03:32:43
     *
     * @param string $id 优惠券ID
     */
    public function receive()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            $user_id = $this->auth->id;
            $coupon = model('app\api\model\wanlshop\Coupon')->get($id);
            if(!$coupon){
                $this->error(__('网络繁忙或优惠券不存在'));
            }
            // 查询此ID领取几张
            $myCouponCount = model('app\api\model\wanlshop\CouponReceive')
                ->where(['coupon_id' => $id, 'user_id' => $user_id])
                ->count();
            // 判断是否发完
            if($coupon['drawlimit'] != 0){
                if($myCouponCount >= $coupon['drawlimit']){
                    $this->error(__('亲，您已领取了'.$myCouponCount.'张，不能在领了！'));
                }
            }
            // 判断是否超出总数量
            if($coupon['grant'] != '-1'){
                if($coupon['alreadygrant'] >= intval($coupon['grant'])  || $coupon['surplus'] == 0){
                    $this->error(__('亲，您来晚了，刚刚被抢完！'));
                }
            }
            // 判断优惠券是否过期
            if($coupon['pretype'] == 'fixed'){
                if(time() > strtotime($coupon['enddate'])){
                    $this->error(__('此张优惠券已经过期了'));
                }
            }
            // 领取优惠券并保留备份
            $result = model('app\api\model\wanlshop\CouponReceive');
            $result->state = 1;
            $result->coupon_id = $id;
            $result->coupon_no = Random::alnum(16);
            $result->user_id = $user_id;
            $result->shop_id = $coupon['shop_id'];
            $result->type = $coupon['type'];
            $result->name = $coupon['name'];
            $result->userlevel = $coupon['userlevel'];
            $result->usertype = $coupon['usertype'];
            $result->price = $coupon['price'];
            $result->discount = $coupon['discount'];
            $result->limit = $coupon['limit'];
            $result->rangetype = $coupon['rangetype'];
            $result->range = $coupon['range'];
            $result->pretype = $coupon['pretype'];
            $result->validity = $coupon['validity'];
            $result->startdate = $coupon['startdate'];
            $result->enddate = $coupon['enddate'];
            $result->save();
            if($result){
                if($coupon['grant'] != '-1'){
                    // 剩余数量
                    $data['surplus'] = $coupon['surplus'] - 1;
                    // 即将过期，强制失效
                    if($coupon['surplus'] == 1){
                        $data['invalid'] = 1;
                    }
                }
                $data['alreadygrant'] = $coupon['alreadygrant'] + 1;
                // 更新优惠券领取+1
                $coupon->allowField(true)->save($data);
                $this->success(__('ok'),['msg' => '恭喜此券，成功领取第 '.($myCouponCount+1).' 张','id' => $result['id']]);
            }else{
                $this->error(__('网络繁忙，领取失败'));
            }
        }
        $this->error(__('非法请求'));
    }


    public function buy_voucher()
    {
        // 设置过滤方法
        $this->request->filter(['strip_tags']);

        if (!$this->request->isPost()) {
            $this->error(__('非法请求'));
        }
        // 获取用户和优惠券信息
        $user = \db("user")->where('id', $this->auth->id)->find();
        if (!$user) {
            $this->error(__('用户不存在'));
        }

        $id = $this->request->post('id');

        if($id == "all"){
            // 创建13星卡券
            $coupon = \db("wanlshop_coupon")->where("type", "voucher")->select();
            $coupon_list = [];
            $all_point = 0;
            foreach ($coupon as $value) {
                $insert_times = ($value["price"] == 1609) ? 1 : 2;
                for ($i = 0; $i < $insert_times; $i++) {
                    $all_point = bcadd($value["points"],$all_point,2);
                    $coupon_list[] = [
                        'state' => 1,
                        'coupon_id' => $value["id"],
                        'coupon_no' => Random::alnum(16),
                        'user_id' => $this->auth->id, // 修正为 $pay[0]['user_id']
                        'shop_id' => $value['shop_id'],
                        'type' => $value['type'],
                        'name' => $value['name'],
                        'userlevel' => $value['userlevel'],
                        'usertype' => $value['usertype'],
                        'price' => $value['price'],
                        'discount' => $value['discount'],
                        'limit' => $value['limit'],
                        'rangetype' => $value['rangetype'],
                        'range' => $value['range'],
                        'pretype' => $value['pretype'],
                        'validity' => $value['validity'],
                        'startdate' => $value['startdate'],
                        'enddate' => $value['enddate']
                    ];
                }
            }
            if($this->auth->score < $all_point){
                $this->error("您的积分不足，无法购买");
            }
            Db::startTrans();
            try {
                // 扣除用户积分
                $updateScore = db("user")
                    ->where("id", $this->auth->id)
                    ->dec('score',$all_point)
                    ->dec('money',$all_point)
                    ->update(["updatetime" => time()]);
                db("user_money_log")->insert([
                    'user_id' =>$this->auth->id,
                    'money' => $all_point, // 操作金额
                    'before' => $this->auth->money, // 原金额
                    'after' => $this->auth->money - $all_point, // 增加后金额
                    'memo' => "购买VIP抵用券(一套)", // 备注
                    'type' => "pay_voucher", // 类型
                    'service_ids' => "", // 业务ID
                    "createtime" => time()
                ]);
                db("wanlshop_coupon_receive")->insertAll($coupon_list);
                if (!$updateScore) {
                    throw new \Exception(__('积分扣除失败'));
                }
                $isSuccess = true;
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }

            if ($isSuccess) {
                $this->success('购买成功');
            }
        }else{
            $coupon = model('app\api\model\wanlshop\Coupon')->where('id', $id)->find();
            if (!$coupon) {
                $this->error(__('网络繁忙或优惠券不存在'));
            }
            // 验证积分是否足够
            if ($this->auth->score < $coupon["points"]) {
                $this->error("您的积分不足，无法购买");
            }
            $isSuccess = false;
            Db::startTrans();
            try {
                // 领取优惠券
                $couponData = [
                    'state' => 1,
                    'coupon_id' => $id,
                    'coupon_no' => Random::alnum(16),
                    'user_id' => $this->auth->id,
                    'shop_id' => $coupon['shop_id'],
                    'type' => $coupon['type'],
                    'name' => $coupon['name'],
                    'userlevel' => $coupon['userlevel'],
                    'usertype' => $coupon['usertype'],
                    'price' => $coupon['price'],
                    'discount' => $coupon['discount'],
                    'limit' => $coupon['limit'],
                    'rangetype' => $coupon['rangetype'],
                    'range' => $coupon['range'],
                    'pretype' => $coupon['pretype'],
                    'validity' => $coupon['validity'],
                    'startdate' => $coupon['startdate'],
                    'enddate' => $coupon['enddate']
                ];

                $result = model('app\api\model\wanlshop\CouponReceive')->save($couponData);
                if (!$result) {
                    throw new \Exception(__('网络繁忙，领取失败'));
                }

                // 扣除用户积分
                $newScore = $this->auth->score - $coupon["points"];
                $newMoney = $this->auth->money - $coupon["points"];
                $updateScore = db("user")->where("id", $this->auth->id)->update([
                    "score" => $newScore,
                    "money" => $newMoney
                ]);
                \db("user_money_log")->insert([
                    "user_id" => $this->auth->id,
                    "money" => -$coupon["points"],
                    "before" => $this->auth->money,
                    "after" => $newMoney,
                    "memo" => "购买VIP抵用券",
                    "type" => "pay_voucher",
                    "createtime" => time()
                ]);

                if (!$updateScore) {
                    throw new \Exception(__('积分扣除失败'));
                }

                Db::commit();
                $isSuccess = true;

            } catch (\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }

            if ($isSuccess) {
                $this->success('购买成功');
            }
        }
    }


    /**
     * 立即使用优惠券
     *
     * @ApiSummary  (WanlShop 优惠券接口立即使用优惠券)
     * @ApiMethod   (GET)
     * 2020年9月16日03:32:43
     * 2020年11月6日10:03:08 1.0.2升级
     *
     * @param string $id 优惠券ID
     */
    public function apply($id = null)
    {
        $coupon = model('app\api\model\wanlshop\CouponReceive')->get($id);
        if($coupon){
            $where['shop_id'] = $coupon['shop_id'];
            $where['price'] = ['EGT', $coupon['limit']];
            // 指定商品
            if($coupon['rangetype'] == 'goods'){
                $where['id'] = ['IN', $coupon['range']];
            }
            // 指定分类
            if($coupon['rangetype'] == 'category'){
                // 1.1.2升级
                $where[] = ['EXP', Db::raw('FIND_IN_SET('.$coupon->range.', shop_category_id)')];
            }
            $list = model('app\api\model\wanlshop\Goods')
                ->where($where)
                ->order('createtime desc')
                ->paginate();
            foreach ($list as $row) {
                $row->shop->visible(['id','avatar','shopname']);
            }
            $this->success('ok', ['coupon' => $coupon, 'goods' => $list]);
        }
        $this->error(__('非正常请求'));
    }

    public function send_user_voucher(){
        // 获取用户和优惠券信息
        $user = \db("user")->where('id', $this->auth->id)->find();
        if (!$user) {
            $this->error(__('用户不存在'));
        }
        $user_liser = \db("user")->field("id,username")->where('direct_push', $this->auth->id)->whereOr('id', $user["pid"])->select();
        if(count($user_liser) == 0){
            $this->error(__('用户有误'));
        }else{
            $this->success('获取成功',$user_liser);
        }
    }


    /**
     * 赠送抵用券
     *
     * @return void
     */
    public function donate_voucher()
    {
        // 设置过滤方法
        $this->request->filter(['strip_tags']);

        if (!$this->request->isPost()) {
            $this->error(__('非法请求'));
        }
        $id = $this->request->post('id');
        $donate_id = $this->request->post('donate_id');
        // 验证参数
        if (empty($id) || empty($donate_id)) {
            $this->error(__('参数不能为空'));
        }
        $user_list = \db("user")->where('mobile', $donate_id)->value("id");
        // 检查是否赠送给自己
        if(!$user_list){
            $this->error(__('用户手机号有误'));
        }elseif($user_list == $this->auth->id) {
            $this->error(__('不能赠送给自己'));
        }else{
            $my_level = $this->get_my_all($this->auth->id);
            if (!in_array($user_list,$my_level)){
                $this->error(__('亲！此号码不是您的好友，无法赠送哦！'));
            }
        }
//        $id = explode(",",$id);
        $isSuccess = false;
        // 使用事务确保数据一致性
        Db::startTrans();
        try {
            // 查询优惠券信息
            $coupon = Db::name('wanlshop_coupon_receive')
                ->where('user_id', $this->auth->id)
                ->where('id', $id)
                ->where('state', 1)
                ->lock(true) // 加锁防止并发修改
                ->find();

            if (!$coupon) {
                throw new \Exception(__('抵用券不存在或已使用'));
            }

            // 检查接收用户是否存在
            $receiver = Db::name('user')->where('id', $user_list)->find();
            if (!$receiver) {
                throw new \Exception(__('接收用户不存在'));
            }

            // 转移优惠券所有权
            $result = Db::name('wanlshop_coupon_receive')
                ->where('id', $id)
                ->update([
                    'user_id' => $user_list,
                    "remark"=>$this->auth->username."=>赠送VIP抵用券",
                    'updatetime' => time()
                ]);

            if (!$result) {
                throw new \Exception(__('赠送失败，请稍后重试'));
            }

            // 记录赠送日志
            Db::name('wanlshop_coupon_donate_log')->insert([
                'coupon_id' => $id,
                'sender_id' => $this->auth->id,
                'receiver_id' => $user_list,
                'createtime' => time()
            ]);

            Db::commit();
            $isSuccess = true;

        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if($isSuccess){
            $this->success(__('赠送成功'));
        }
    }

    public function get_my_all($userid){
        $up = [];
        $dowm = [];
        $z = 0;
        $now_arr = 0;
        for ($i = 0;$i < 1;){
            if ($z == 0){
                $now_arr = db("user")->where("id",$userid)->value("pid");
            }else{
                $now_arr = db("user")->where("id",$now_arr)->value("pid");
            }
            $z ++;
            if ($now_arr == null || in_array($now_arr,$up)){
                $i = 10;
            }else{
                $up[] = $now_arr;
            }
        }
        $now_arr = [];
        $z = 0;
        for ($j = 0;$j < 1;){
            if ($z == 0){
                $now_arr = db("user")->where("pid",$userid)->column("id");
            }else{
                $now_arr = db("user")->where("pid","in",$now_arr)->column("id");
            }
            $z ++;
            if (count($now_arr) == 0){
                $j = 10;
            }else{
                $dowm = array_merge($dowm,$now_arr);
            }
        }
        return array_merge($dowm,$up);
    }


    /**
     * @return void
     * 获取用户积分
     */
    public function get_score()
    {
        $this->success($this->auth->score);
    }

    /**
     * @return void
     * 购买所有券
     */
    public function buy_all_coupon()
    {
        Log::debug('Wanlpay notify', ["success"=>"插入成功"]);
        $coupon = \db("wanlshop_coupon")->where("type", "voucher")->select();
        $coupon_list = [];
        $total_money = 0;
        foreach($coupon as $value) {
            $insert_times = ($value["price"] == 1609) ? 1 : 2;
            for ($i = 0; $i < $insert_times; $i++) {
                $coupon_list[] = [
                    'state' => 1,
                    'coupon_id' => $value["id"],
                    'coupon_no' => Random::alnum(16),
                    'user_id' => $this->auth->id,
                    'shop_id' => $value['shop_id'],
                    'type' => $value['type'],
                    'name' => $value['name'],
                    'userlevel' => $value['userlevel'],
                    'usertype' => $value['usertype'],
                    'price' => $value['price'],
                    'discount' => $value['discount'],
                    'limit' => $value['limit'],
                    'rangetype' => $value['rangetype'],
                    'range' => $value['range'],
                    'pretype' => $value['pretype'],
                    'validity' => $value['validity'],
                    'startdate' => $value['startdate'],
                    'enddate' => $value['enddate']
                ];
                $total_money += $value["price"];
            }
        }
        if($this->request->isPost()){
            \db("wanlshop_coupon_receive")->insertAll($coupon_list);
        }
        $this->success("获取成功",["title"=>"一键购买13星vip券","price"=>$total_money]);
    }

    public function donate_voucher_log(){
        $list = [];
        $send = db("wanlshop_coupon_donate_log")->where("sender_id",$this->auth->id)->select();
        if(count($send) == 0){
            $list["send"] = [];
        }
        $receive = db("wanlshop_coupon_donate_log")->where("receiver_id",$this->auth->id)->select();
        if(count($receive) == 0){
            $list["receive"] = [];
        }
        foreach ($send as $v){
            $list["send"][] = [
                "id"=>$v["id"],
                "coupon" => db("wanlshop_coupon_receive")->where("id",$v["coupon_id"])->value("name"),
                "sender_name" => db("user")->where("id",$v["sender_id"])->value("nickname"),
                "receiver_name" => db("user")->where("id",$v["receiver_id"])->value("nickname"),
                "createtime" => date("Y-m-d H:i:s",$v["createtime"])
            ];
        }
        foreach ($receive as $v){
            $list["receive"][] = [
                "id"=>$v["id"],
                "coupon" => db("wanlshop_coupon_receive")->where("id",$v["coupon_id"])->value("name"),
                "sender_name" => db("user")->where("id",$v["sender_id"])->value("nickname"),
                "receiver_name" => db("user")->where("id",$v["receiver_id"])->value("nickname"),
                "createtime" => date("Y-m-d H:i:s",$v["createtime"])
            ];
        }
        $this->success("获取成功",$list);
    }

}